import G6 from '@antv/g6';
import fonts from '@/assets/iconfont/iconfont.json';

export function uuid(len = 8) {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  const uuid = [];
  let radix = chars.length;
  if (len) {
    radix = Math.min(radix, len);
  }
  for (let i = 0; i < radix; i++) {
    uuid[i] = chars[0 | Math.random() * radix];
  }
  return uuid.join('');
}

/**
* format the string
* @param {string} str The origin string
* @param {number} maxWidth max width
* @param {number} fontSize font size
* @return {string} the processed result
*/
export function fittingString(str, maxWidth, fontSize) {
  const ellipsis = "...";
  const ellipsisLength = G6.Util.getTextSize(ellipsis, fontSize)[0];
  let currentWidth = 0;
  let res = str;
  const pattern = new RegExp("[\u4E00-\u9FA5]+"); // distinguish the Chinese charactors and letters
  str.split("").forEach((letter, i) => {
    if (currentWidth > maxWidth - ellipsisLength) return;
    if (pattern.test(letter)) {
      // Chinese charactors
      currentWidth += fontSize;
    } else {
      // get the width of single letter according to the fontSize
      currentWidth += G6.Util.getLetterWidth(letter, fontSize);
    }
    if (currentWidth > maxWidth - ellipsisLength) {
      res = `${str.substr(0, i)}${ellipsis}`;
    }
  });
  return res;
};

export const icons = fonts.glyphs.map((icon) => ({ name: icon.name, unicode: String.fromCodePoint(icon.unicode_decimal), iconClass: icon.font_class }));
export const getIcon = (type, keywords = "name") => (icons.find((icon) => icon[keywords] === type) || { unicode: '', [keywords]: 'default' }).unicode;
export const iconList = Array.from({ length: icons.length }, (_, index) => ({ label: icons[index].name, icon: icons[index].unicode, className: icons[index].iconClass }));

/**
* @description 返回0-1的 rate
* @param {duration} duration 动画时长
* @param {callback} callback 动画帧回调函数
* @param {finish} finish 动画完成回调函数
* @returns {void}
*/
export function expandAnimate({ duration, callback, finish }) {
  const startTime = Date.now();
  const endTime = startTime + duration;

  function step(rate) {
    callback instanceof Function && callback(rate);
    const currentTime = Date.now();
    const progress = (currentTime - startTime) / +duration;

    if (currentTime < endTime) {
      requestAnimationFrame(step.bind(null, progress));
    } else {
      finish instanceof Function && finish();
    }
  }
  step();

}
/**
* @description 返回1-0的 rate
* @param {number} duration 动画时长
* @param {function} callback 动画帧回调函数
* @param {function} finish 动画完成回调函数
* @returns {void}
*/
export function shrinkAnimate({ duration, callback, finish }) {
  const startTime = Date.now();
  const endTime = startTime + duration;

  function step(rate) {
    callback instanceof Function && callback(rate);
    const currentTime = Date.now();
    const progress = (endTime - currentTime) / +duration;

    if (currentTime < endTime) {
      requestAnimationFrame(step.bind(null, progress));
    } else {
      finish instanceof Function && finish();
    }
  }
  step();

}